package dao;

import Utils.JDBCUtil;
import pojo.Course;
import pojo.Shop;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class ShopDao {
    public int insertOne(Shop shop) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into shop(account,course) values(?,?)";
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, shop.getAccount());
            preparedStatement.setString(2, shop.getCourse());

            count = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(preparedStatement, connection);
        }
        return count;
    }

    public int deleteOne(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from shop where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            count = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(preparedStatement, connection);
        }
        return count;
    }

    public ArrayList<Shop> findShop(String account) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ArrayList<Shop> courses = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT * FROM shop  WHERE shop.account = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, account);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                courses.add(new Shop(resultSet.getInt("id"), resultSet.getString("account"), resultSet.getString(
                        "course"), resultSet.getInt("state")));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(resultSet, preparedStatement, connection);
        }
        return courses;

    }
    public ArrayList<Course> findGoods(String account) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ArrayList<Course> courses = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT course.* FROM shop JOIN course ON shop.course = course.id  WHERE shop.account = ? and state = 1";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, account);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                courses.add(new Course(resultSet.getInt("id"), resultSet.getString("name"), resultSet.getInt("price"), resultSet.getString("imagepath")));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(resultSet, preparedStatement, connection);
        }
        return courses;
    }

    public void changeState(String account) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "update shop set state=0 where account = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, account);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(resultSet, preparedStatement, connection);
        }
    }

    public void changeOne(String id, int state) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "update shop set state = ? where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, state);
            preparedStatement.setString(2, id);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(preparedStatement, connection);
        }
    }
}
